cmake_minimum_required(VERSION 3.14)
set(PROJECT_NAME "calcengine_flutter_libs")
project(${PROJECT_NAME} LANGUAGES C CXX)

# 设置 calculator 下载链接 11.2509.0
set(CALCULATOR_URL "https://github.com/microsoft/calculator/archive/637391a10855370344fae365b938bf27de80de6f.zip")

# This value is used when generating builds using this plugin, so it must
# not be changed
set(PLUGIN_NAME "calcengine_flutter_libs_plugin")

add_library(${PLUGIN_NAME} SHARED
  "calcengine_flutter_libs_plugin.cpp"
)

apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES
  CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
  "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter flutter_wrapper_plugin)

include(FetchContent)
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
  FetchContent_Declare(
    calculator
    URL ${CALCULATOR_URL} 
    DOWNLOAD_EXTRACT_TIMESTAMP NEW
  )
else()
  FetchContent_Declare(
    calculator
    URL ${CALCULATOR_URL}
  )
endif()
FetchContent_MakeAvailable(calculator)

file(GLOB_RECURSE SOURCES
  "${calculator_SOURCE_DIR}/src/CalcManager/CEngine/*.cpp"
  "${calculator_SOURCE_DIR}/src/CalcManager/Ratpack/*.cpp"
  "${calculator_SOURCE_DIR}/src/CalcManager/*.cpp"
)
add_library(calcengine SHARED
  ${SOURCES}
)

target_compile_features(calcengine PUBLIC cxx_std_17)
target_include_directories(calcengine PRIVATE "${calculator_SOURCE_DIR}")
target_compile_options(calcengine PRIVATE "$<$<NOT:$<CONFIG:Debug>>:-O2>" "/w")
target_precompile_headers(calcengine PRIVATE
  "${calculator_SOURCE_DIR}/src/CalcManager/pch.h"
)
target_include_directories(calcengine PRIVATE
  "${calculator_SOURCE_DIR}/src/CalcManager"
  "${calculator_SOURCE_DIR}/src/CalcManager/Ratpack"
  "${calculator_SOURCE_DIR}/src/CalcManager/CEngine"
  "${calculator_SOURCE_DIR}/src/CalcManager/Header Files"
)

# Ensure calcengine actually gets build
add_dependencies(${PLUGIN_NAME} calcengine)

# List of absolute paths to libraries that should be bundled with the plugin
set(calcengine_flutter_libs_bundled_libraries
  "$<TARGET_FILE:calcengine>"
  PARENT_SCOPE
)
